﻿using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Utility.Helper;

namespace BLL
{
    public class QX_PLANYEAR_DETAILBLL : EntityBLL<QX_PLANYEAR_DETAIL>
    {
        public QX_PLANYEAR_DETAILBLL(Service service)
        {
            this.Service = service;
        }

        public QX_PLANYEAR_DETAILBLL()
        {
        }

        public override bool Insert(Model.QX_PLANYEAR_DETAIL entity)
        {
            entity.N_YDETAIL_ID = GetSequence("SEQ_N_YDETAIL_ID");
            var sql = @"INSERT INTO QX_PLANYEAR_DETAIL(
                            N_YDETAIL_ID,N_YEARAPP_ID,N_PROID,
                            C_PRONAME,N_AMOUNT,C_REMARK,
                            N_OPERATORID,D_OPERATORTIME,N_BUSINESS_ID,
                            N_TYPE,C_PRONUM,N_TYPE2,
                            N_NUM1,N_NUM2,N_PARENTID,
                            N_IF_DETAILS,C_DETAILS_CODE)
                        VALUES(
                            :N_YDETAIL_ID,:N_YEARAPP_ID,:N_PROID,
                            :C_PRONAME,:N_AMOUNT,:C_REMARK,
                            :N_OPERATORID,:D_OPERATORTIME,:N_BUSINESS_ID,
                            :N_TYPE,:C_PRONUM,:N_TYPE2,
                            :N_NUM1,:N_NUM2,:N_PARENTID,
                            :N_IF_DETAILS,:C_DETAILS_CODE)";
            var parms = new List<Tuple<string, string, string>>();
            parms.Add(":N_YDETAIL_ID", entity.N_YDETAIL_ID, "int");
            parms.Add(":N_YEARAPP_ID", entity.N_YEARAPP_ID, "int");
            parms.Add(":N_PROID", entity.N_PROID, "int");
            parms.Add(":C_PRONAME", entity.C_PRONAME, "string");
            parms.Add(":N_AMOUNT", entity.N_AMOUNT, "decimal");
            parms.Add(":C_REMARK", entity.C_REMARK, "string");
            parms.Add(":N_OPERATORID", entity.N_OPERATORID, "int");
            parms.Add(":D_OPERATORTIME", entity.D_OPERATORTIME, "datetime");
            parms.Add(":N_BUSINESS_ID", entity.N_BUSINESS_ID, "int");
            parms.Add(":N_TYPE", entity.N_TYPE, "int");
            parms.Add(":C_PRONUM", entity.C_PRONUM, "string");
            parms.Add(":N_TYPE2", entity.N_TYPE2, "int");
            parms.Add(":N_NUM1", entity.N_NUM1, "int");
            parms.Add(":N_NUM2", entity.N_NUM2, "int");
            parms.Add(":N_PARENTID", entity.N_PARENTID, "int");
            parms.Add(":N_IF_DETAILS", entity.N_IF_DETAILS, "int");
            parms.Add(":C_DETAILS_CODE", entity.C_DETAILS_CODE, "string");
            return NoQuery(sql, parms) > 0 ? true : false;
        }

        public override bool Update(QX_PLANYEAR_DETAIL entity)
        {
            var sql = @"UPDATE QX_PLANYEAR_DETAIL T1
                        SET
                            T1.N_YEARAPP_ID = :N_YEARAPP_ID,
                            T1.N_PROID = :N_PROID,
                            T1.C_PRONAME = :C_PRONAME,
                            T1.N_AMOUNT = :N_AMOUNT,
                            T1.C_REMARK = :C_REMARK,
                            T1.N_OPERATORID = :N_OPERATORID,
                            T1.D_OPERATORTIME :D_OPERATORTIME,
                            T1.N_BUSINESS_ID = :N_BUSINESS_ID,
                            T1.N_TYPE = :N_TYPE,
                            T1.C_PRONUM = :C_PRONUM,
                            T1.N_TYPE2 = :N_TYPE2,
                            T1.N_NUM1 = :N_NUM1,
                            T1.N_NUM2 = :N_NUM2,
                            T1.N_PARENTID = :N_PARENTID,
                            T1.N_IF_DETAILS =:N_IF_DETAILS,
                            T1.C_DETAILS_CODE = :C_DETAILS_CODE
                        WHERE
                            T1.N_YDETAIL_ID = ?N_YDETAIL_ID";
            var parms = new List<Tuple<string, string, string>>();
            parms.Add(":N_YDETAIL_ID", entity.N_YDETAIL_ID, "int");
            parms.Add(":N_YEARAPP_ID", entity.N_YEARAPP_ID, "int");
            parms.Add(":N_PROID", entity.N_PROID, "int");
            parms.Add(":C_PRONAME", entity.C_PRONAME, "string");
            parms.Add(":N_AMOUNT", entity.N_AMOUNT, "decimal");
            parms.Add(":C_REMARK", entity.C_REMARK, "string");
            parms.Add(":N_OPERATORID", entity.C_REMARK, "int");
            parms.Add(":D_OPERATORTIME", entity.D_OPERATORTIME, "datetime");
            parms.Add(":N_BUSINESS_ID", entity.N_BUSINESS_ID, "int");
            parms.Add(":N_TYPE", entity.N_TYPE, "int");
            parms.Add(":C_PRONUM", entity.C_PRONUM, "string");
            parms.Add(":N_TYPE2", entity.N_TYPE2, "int");
            parms.Add(":N_NUM1", entity.N_NUM1, "int");
            parms.Add(":N_NUM2", entity.N_NUM2, "int");
            parms.Add(":N_PARENTID", entity.N_PARENTID, "int");
            parms.Add(":N_IF_DETAILS", entity.N_IF_DETAILS, "int");
            parms.Add(":C_DETAILS_CODE", entity.C_DETAILS_CODE, "string");
            return NoQuery(sql, parms) > 0 ? true : false;
        }

        public override QX_PLANYEAR_DETAIL Find(object key)
        {
            throw new NotImplementedException();
        }

        public List<QX_PLANYEAR_DETAIL> GetByYearID(decimal id)
        {
            return Where("N_YEARAPP_ID = " + id.ToString(), " N_YDETAIL_ID ");
        }

        public override List<QX_PLANYEAR_DETAIL> ConvertEntity(DataTable _table)
        {
            var entitys = new List<QX_PLANYEAR_DETAIL>();
            foreach (var item in _table.AsEnumerable())
            {
                var entity = new QX_PLANYEAR_DETAIL();
                entitys.Add(entity);
                entity.N_YDETAIL_ID = item.GetValue<int>("N_YDETAIL_ID");
                entity.N_YEARAPP_ID = item.GetValue<int>("N_YEARAPP_ID");
                entity.N_PROID = item.GetValue<int>("N_PROID");
                entity.C_PRONAME = item.GetValue<string>("C_PRONAME");
                entity.N_AMOUNT = item.GetValue<decimal>("N_AMOUNT");
                entity.C_REMARK = item.GetValue<string>("C_REMARK");
                entity.N_OPERATORID = item.GetValue<int>("N_OPERATORID");
                entity.D_OPERATORTIME = item.GetValue<DateTime>("D_OPERATORTIME");
                entity.N_BUSINESS_ID = item.GetValue<int>("N_BUSINESS_ID");
                entity.N_TYPE = item.GetValue<int>("N_TYPE");
                entity.C_PRONUM = item.GetValue<string>("C_PRONUM");
                entity.N_TYPE2 = item.GetValue<int>("N_TYPE2");
                entity.N_NUM1 = item.GetValue<int>("N_NUM1");
                entity.N_NUM2 = item.GetValue<int>("N_NUM2");
                entity.N_PARENTID = item.GetValue<int>("N_PARENTID");
                entity.N_IF_DETAILS = item.GetValue<int?>("N_IF_DETAILS");
                entity.C_DETAILS_CODE = item.GetValue<string>("C_DETAILS_CODE");
            }
            return entitys;
        }

        #region 公司汇总新增

        public void LoadByApplyId(int applyId, int businessId)
        {
            //查询资金申请计划Project
            //查询Proejct信息表
            var projectBll = new QS_PROJECTBLL();

            var entitys = projectBll.GetAllProject();
            //是否使用通用公司模板
            if (entitys.Where(a => a.CMY_ID == User.USER_COMPANY).Count() == 0)
            {
                entitys = entitys.Where(a => a.CMY_ID == 0).ToList();
            }
            else
            {
                entitys = entitys.Where(a => a.CMY_ID == User.USER_COMPANY).ToList();
            }
            Go(0, entitys, applyId, businessId);
            //递归添加
        }

        public void Go(int pid, List<QS_PROJECT> entitys, int applyId, int businessId)
        {
            var arry = entitys.Where(a => a.N_PARENTID == pid);
            var projectDetailsBll = new QX_PLANYEAR_DETAILBLL();
            foreach (var entity in arry.OrderBy(a => a.N_NUM1).ThenBy(a => a.N_NUM2))
            {
                var item = new QX_PLANYEAR_DETAIL();
                item.C_PRONUM = entity.C_PRONUM;
                item.C_PRONAME = entity.C_PRONAME;
                item.C_REMARK = entity.C_REMARK;
                item.D_OPERATORTIME = DateTime.Now;
                item.N_AMOUNT = 0;
                item.N_BUSINESS_ID = businessId;
                item.N_NUM1 = entity.N_NUM1;
                item.N_NUM2 = entity.N_NUM2;
                item.N_OPERATORID = User.USER_ID;
                item.N_PARENTID = entity.N_PARENTID;
                item.N_PROID = entity.N_PROID;
                item.N_TYPE = entity.N_TYPE;
                item.N_TYPE2 = entity.N_TYPE2;
                item.N_YDETAIL_ID = 0;
                item.N_YEARAPP_ID = applyId;
                item.N_IF_DETAILS = (int)entity.N_IF_DETAILS;
                item.C_DETAILS_CODE = entity.C_DETAILS_CODE;
                projectDetailsBll.Insert(item);
                Go((int)item.N_PROID, entitys, applyId, businessId);
            }
        }

        #endregion 公司汇总新增
    }
}